﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swork03_28.Api.Application.Commands.FarmedCommand;
using Swork03_28.Api.Application.Commands.TraceableCommand;
using Swork03_28.Domain.Dto;
using Swork03_28.Domain.TraceableInfos;

namespace Swork03_28.Api.Controllers
{
    /// <summary>
    /// 溯源控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class TraceablesController : ControllerBase
    {
        private readonly IMediator mediator;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="mediator"></param>
        public TraceablesController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public IActionResult FIleUpLoad(IFormFile file)
        {
            var fname = Directory.GetCurrentDirectory() + @"/wwwroot/Img" + file.FileName;
            using (FileStream fs = new FileStream(fname, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return Ok(file.FileName);
        }

        /// <summary>
        /// 获取溯源信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PageInfo> GetTraceables([FromQuery] TraceableSelectCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 添加溯源信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> TraceableCreate(TraceableCreateCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 获取商品信息(绑定下拉框)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<GoodsInfo>> GetGoodsInfos([FromQuery] GoodsSelectCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 批删，单删(逻辑删除)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DeleteTraceables([FromQuery] TraceableDeleteCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Traceables> BackfillTraceables([FromQuery] TraceableBackfillCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateTraceable(TraceableUpdateCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 获取溯源详情
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TraceableDto> GetTraceablesDetail([FromQuery] TraceableDetailCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 添加产品检验报告
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> TraceableCreateTestImg(TraceableCreateTestImgCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 删除检验证图片
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> TraceableDelImg([FromQuery] TraceableDelImgCommand command)
        {
            return await mediator.Send(command);
        }


    }
}
